package com.jcompressor.model.resources;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;

import com.jcompressor.model.config.Resource;
import com.jcompressor.type.ResourceType;

public class ResourceConfiguration implements Serializable {
	
	private static final long serialVersionUID = -905791591248965187L;
	
	private List<Resource> resources;
	private List<Template> templates;
	
	public ResourceConfiguration() {
		this.resources = new ArrayList<Resource>();
		this.templates = new ArrayList<Template>();
	}
	
	public List<Resource> getResources() {
		return this.resources;
	}
	
	public List<Resource> getResources(final ResourceType type) {
		final List<Resource> resources = new ArrayList<Resource>();
		for (final Resource resource : this.resources) {
			if (ObjectUtils.equals(resource.getType(), type)) {
				resources.add(resource);
			}
		}
		
		return resources;
	}
	
	public List<Template> getTemplates() {
		return this.templates;
	}
	
	public void setResources(final List<Resource> resources) {
		this.resources = resources;
	}
	
	public void setTemplates(final List<Template> templates) {
		this.templates = templates;
	}
	
	public Resource getResource(final String id) {
		for (final Resource resource : this.resources) {
			if (StringUtils.equals(resource.getId(), id) || StringUtils.equals(resource.getName(), id)) {
				return resource;
			}
		}
		
		return null;
	}
	
	public Resource getResource(final String name, final String library) {
		for (final Resource resource : this.resources) {
			if (StringUtils.equals(resource.getName(), name) && StringUtils.equals(resource.getLibrary(), library)) {
				return resource;
			}
		}
		
		return null;
	}
	
	public Template getTemplate(final String id) {
		for (final Template template : this.templates) {
			if (StringUtils.equals(template.getId(), id)) {
				return template;
			}
		}
		
		return null;
	}
	
	public void addResource(final Resource resource) {
		this.resources.add(resource);
	}
	
	public void addTemplate(final Template template) {
		this.templates.add(template);
	}
	
	public void updateResource(final Resource resource) {
		if (this.containsResource(resource)) {
			final Resource res = this.getResource(resource.getId());
			this.resources.remove(res);
			this.resources.add(resource);
		}
	}
	
	public void removeResource(final Resource resource) {
		if (this.containsResource(resource)) {
			final Resource res = this.getResource(resource.getId());
			this.resources.remove(res);
		}
	}
	
	public boolean containsResource(final Resource resource) {
		return (this.getResource(resource.getId()) != null);
	}
	
	public boolean containsTemplate(final Template template) {
		return this.containsTemplate(template.getId());
	}
	
	public boolean containsTemplate(final String templateId) {
		return (this.getTemplate(templateId) != null);
	}
	
}